﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeManagement.Statistic
{
    public partial class StatisticTree : Form
    {
        public StatisticTree()
        {
            InitializeComponent();
        }

        private void cbbWard1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!loadCompleted)
                return;
            using (var context = new TreeDBEntities())
            {
                if (sender == cbbWard1 && cbbRoad.DataSource!=null)
                {
                    lbTotalTree.Text = context.FindTreeByWard((cbbWard1.SelectedItem as Ward).ID).ToList<Tree>().Count.ToString();
                }
                else if ((sender == cbbWard2) || (sender == cbbTreeType))
                {
                    int wardID = (cbbWard2.SelectedItem as Ward).ID;
                    lbTotalTreeWardType.Text = context.FindTreeByWardByType(wardID, (cbbTreeType.SelectedItem as TreeType).ID).ToList<Tree>().Count.ToString();
                }
                else if (sender == cbbRoad)
                {
                    var tree = context.Trees.Find(Convert.ToInt32(txtTreeID.Text));
                    if (tree != null)
                    {                        
                        Road r = (cbbRoad.SelectedItem as Road);
                        lbDistanceTreeRoad.Text = tree.Point.Distance(r.Line).ToString();
                    }
                }
            }
            
        }
        private bool loadCompleted = false;
        private void StatisticTree_Load(object sender, EventArgs e)
        {
            using (var context = new TreeDBEntities())
            {
                cbbTreeType.DataSource = context.TreeTypes.ToList();
                cbbRoad.DataSource = context.Roads.ToList();
                cbbWard1.DataSource = context.Wards.ToList();
                cbbWard2.DataSource = context.Wards.ToList();
                loadCompleted = true;
            }
        }

        

        
    }
}
